import sys
import os

class Config(object):
    pass

def config_read(filename):
    d = {}
    # FIXME: security: arbitrary code execution
    execfile(filename, d)
    c = Config()
    c.__dict__ = d
    return c

def system_config_path():
    p = sys.platform
    if p.startswith('linux') or p.startswith('unix'):
        config_dir = '/etc'
    elif p.startswith('win'):
        config_dir = 'c:\\'
    else:
        config_dir = '/etc'
    return os.path.join(config_dir, 'itrsrpc.py')

def user_config_path():
    return os.path.expanduser('~/.itrsrpc.py')

class ConfigMissing(Exception):
    pass

def load():
    config_paths = [ user_config_path(), system_config_path() ]
    for i in config_paths:
        try:
            c = config_read(i)
        except IOError:
            pass
        else:
            return c
    else:
        raise ConfigMissing()

    return config_read(system_config_path())
